import { createApp } from 'vue';
import MaplibPopup from './MaplibPopup.vue'; //引入弹框组件
import mitt from 'mitt';

export function geomFirst(obj) {
  if (obj && obj.features && obj.features.length > 0) {
    return obj.features[0];
  }

  return null;
}

export async function tableData(obj, infoApi) {
  const gf = geomFirst(obj);
  if (gf) {
    if (gf.properties) {
      return await infoApi(gf.properties);
    }
  }

  return {};
}

export async function createPopUp(features, tableSchema, clickPopUpOk, infoApi) {
  const obj = await tableData(features, infoApi);
  const theApp = createApp(MaplibPopup, {
    obj: obj,
    tableSchema: tableSchema,
  });
  const node = document.createElement('div', {});
  const emitter = mitt();
  emitter.on('clickok', clickPopUpOk);
  theApp.provide('theMitt', emitter);
  const inst = theApp.mount(node);
  return inst.$el;
}
